﻿using System;
using System.Collections.Generic;
using System.Data;
using Smatime.Web.DAO.SmatimeDatasetTableAdapters;
using Smatime.Web.Models;

namespace Smatime.Web.DAO
{
    public class UserAccessRepository :
        BaseRepository<UserAccess, SmatimeDataset.UserAccessDataTable, UserAccessTableAdapter>
    {
        /// <summary>
        ///     Login Check
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public UserAccess Login(string email, string password)
        {
            using (var adapter = new UserAccessTableAdapter())
            {
                var list = new List<UserAccess>();
                using (var table = adapter.GetDataByLogin(email, password))
                {
                    var row = table.Rows[0];
                    return (UserAccess) (new UserAccess().FromDataRow(row));
                }
            }
        }


        public UserAccess Login(UserAccess userAccess)
        {
            using (var adapter = new ListUserAccessByCompanyIdTableAdapter())
            {
                var list = new List<StaffStore>();
                using (
                    var table =
                        adapter.GetDataByUsernamePassword(userAccess.Email, userAccess.Password))
                {
                    var row = table.Rows[0];
                    return (UserAccess) (new UserAccess().FromDataRow(row));
                }
            }
        }

        public bool ResetPassword(UserAccess userAccess)
        {
            using (var adapter = new UserAccessTableAdapter())
            {
                return adapter.ResetPassword(userAccess.Password, userAccess.Email) == 1;
            }
        }

        public bool UpdateResetCode(string email, string resetCode, DateTime? createdTime)
        {
            using (var adapter = new UserAccessTableAdapter())
            {
                return adapter.UpdateResetCode(resetCode, createdTime, email) == 1;
            }
        }


        public UserAccess GetByEmailAndResetCode(string email, string resetCode)
        {
            using (var adapter = new UserAccessTableAdapter())
            {
                var list = new List<StaffStore>();
                using (
                    var table = adapter.GetUserAccessByEmailAndResetCode(email, resetCode)
                    )
                {
                    var row = table.Rows[0];
                    return (UserAccess) (new UserAccess().FromDataRow(row));
                }
            }
        }

        /// <summary>
        ///     Email Check
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public UserAccess CheckEmail(string email)
        {
            using (var adapter = new UserAccessTableAdapter())
            {
                var list = new List<UserAccess>();
                using (var table = adapter.GetCheckEmail(email))
                {
                    var row = table.Rows[0];
                    return (UserAccess) (new UserAccess().FromDataRow(row));
                }
            }
        }

        public UserAccess GetByEmail(string email)
        {
            using (var adapter = new UserAccessTableAdapter())
            {
                using (var table = adapter.GetDataByEmail(email))
                {
                    if (table.Count == 0)
                    {
                        return null;
                    }
                    return (UserAccess)(new UserAccess().FromDataRow(table.Rows[0]));
                }
            }
        }
    }
}